import util from '@/libs/util';
import dateUtil from '../../util/getDate';
import regs from '../../util/formReg';

export default {
    data() {
        return {
            pageSize: 15,
            pageIndex: 1,
            total: 0,
            columns: [{
                width: 55,
                title: '#',
                type: 'selection'
            }, {
                title: '活动名称',
                key: 'name'
            }, {
                title: '活动地点',
                key: 'place'
            }, {
                title: '活动开始时间',
                key: 'startTime',
                render: function (h, params) {
                    let text = '';
                    if (regs.isNotEmpty(params.row.startTime)) {
                        text = dateUtil.getDate(params.row.startTime, 'date');
                    }
                    return h('span', {}, text);
                }
            }, {
                title: '活动结束时间',
                key: 'endTime',
                render: function (h, params) {
                    let text = '';
                    if (regs.isNotEmpty(params.row.startTime)) {
                        text = dateUtil.getDate(params.row.startTime, 'date');
                    }
                    return h('span', {}, text);
                }
            }, {
                title: '活动人数/已报名人数',
                key: 'k',
                render: function (h, params) {
                    let applyPeople = params.row.applyPeople;
                    let limitedPeople = params.row.limitedPeople;
                    return h('span', {}, limitedPeople + '人/' + applyPeople + '人');
                }
            }, {
                title: '活动费用(元)',
                key: 'amt'
            }, {
                title: '活动创建时间',
                key: 'createTime',
                render: function (h, params) {
                    let text = '';
                    if (regs.isNotEmpty(params.row.createTime)) {
                        text = dateUtil.getDate(params.row.createTime, 'time');
                    }
                    return h('span', {}, text);
                }
            }, {
                title: '活动状态',
                key: 'k',
                render: function (h, params) {
                    let status = params.row.status;
                    let text = '未知';
                    let style = {
                        'font-weight': 'bold'
                    };
                    switch (status) {
                        case 'APPLY':
                            text = '报名中';
                            style.color = 'green';
                            break;
                        case 'ACTIVITY':
                            text = '活动进行中';
                            style.color = 'blue';
                            break;
                        case 'END':
                            text = '活动结束';
                            style.color = 'red';
                            break;
                        case 'NOTRELEASE':
                            text = '未发布';
                            style.color = 'grey';
                            break;
                        default:
                            break;
                    }
                    return h('span', {
                        style: style
                    }, text);
                }
            }, {
                title: '操作',
                key: 'opt',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.formData = params.row;
                                    this.addVisible = true;
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            on: {
                                click: () => {
                                    let obj = [];
                                    obj.push(params.row.id);
                                    this.doDel(obj);
                                }
                            }
                        }, '删除')
                    ]);
                }
            }],
            dataList: [],
            queryData: {},
            addVisible: false,
            formData: {
                amt: 0,
                limitedPeople: 0
            },
            formDataRules: {
                name: [
                    {required: true, message: '请输入活动名称', trigger: 'blur'}
                ],
                amt: [
                    {required: true, message: '请输入活动费用', trigger: 'blur', type: 'number'}
                ]
            },
            saveLoading: false,
            selection: [],
        };
    },
    mounted() {
        this.init();
    },
    methods: {
        init: function () {
            this.doQuery();
        },
        doQuery: function () {
            let queryData = this.queryData;
            queryData.page = this.pageIndex;
            queryData.size = this.pageSize;
            queryData.status='NOTRELEASE';
            util.ajax.post('/busine/activity/list', util.toFormData(queryData)).then(res => {
                if (res.data.code == 200) {
                    this.dataList = res.data.data.list;
                    this.total = res.data.data.total;
                } else {
                    this.$Message.error(res.data.message);
                }
            });
        },
        showAdd: function () {
            this.$refs['formData'].resetFields();
            this.addVisible = true;
        },
        doSave: function () {
            this.saveLoading = true;
            this.$refs['formData'].validate((valid) => {
                if (valid) {
                    util.ajax.post('/busine/activity/save', util.toFormData(this.formData)).then(res => {
                        this.saveLoading = false;
                        if (res.data.code == 200) {
                            this.$Message.success(res.data.message);
                            this.addVisible = false;
                            this.doQuery();
                        } else {
                            this.$Message.warning(res.data.message);
                        }
                    });
                } else {
                    this.$Message.error('请检查必填项');
                }
            });
        },
        doDel: function (ids) {
            if (ids) {
                this.$Modal.confirm({
                    title: '提示',
                    content: '确认删除？',
                    onOk: () => {
                        util.ajax.post('/busine/activity/delete', util.toFormData({ids: ids.join(',')})).then(res => {
                            if (res.data.code == 200) {
                                if (res.data.message == 'SUCCESS') {
                                    this.$Message.success('删除成功！');
                                    this.pageIndex = 1;
                                    this.init();
                                } else {
                                    this.$Message.warning(res.data.message || '未知错误！');
                                }
                            }
                        });
                    },
                    onCancel: () => {

                    }
                });
            } else {
                this.$Modal.error({content: '请选择删除项!'});
            }
        },
        whenSelectRow: function (selection) {
            this.selection = selection;
        },
        doDelBath: function (selection) { // 批量删除
            let list = this.selection;
            if (list.length == 0) {
                this.$Message.error('请选择删除项目');
                return;
            }
            var ids = [];
            for (let i in list) {
                ids.push(list[i].id);
            }
            this.doDel(ids);
        },
        doReleaseBath: function (selection) {
            let list = this.selection;
            if (list.length == 0) {
                this.$Message.warning('请选择发布项目');
                return;
            }
            var ids = [];
            for (let i in list) {
                ids.push(list[i].id);
            }
            this.doRelease(ids);
        },
        doRelease: function (ids) {
            if (ids) {
                this.$Modal.confirm({
                    title: '提示',
                    content: '确认发布？',
                    onOk: () => {
                        util.ajax.post('/busine/activity/doRelease', util.toFormData({ids: ids.join(',')})).then(res => {
                            if (res.data.code == 200) {
                                this.$Message.success(res.data.message);
                                this.pageIndex = 1;
                                this.init();
                            } else {
                                this.$Message.error(res.data.message || '未知错误！');
                            }
                        });
                    },
                    onCancel: () => {

                    }
                });
            } else {
                this.$Modal.warning({content: '请选择发布项!'});
            }
        }
    }
};
